<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ShopOrder extends Model
{
    //
    use SoftDeletes;
    protected $table = "shop_order";

    //生成订单号
    public static function make_no()
    {
        // 订单流水号前缀
        $prefix = "B".date('YmdHis');
        for ($i = 0; $i < 10; $i++) {
            // 随机生成 6 位的数字
            $no = $prefix.str_pad(random_int(0, 999999), 6, '0', STR_PAD_LEFT);
            // 判断是否已经存在
            if (!empty(self::where('sn',$no)->first())) {
                $no = self::make_no();
            }
        }
        return $no;
    }

}
